const BaseController = require('../core/base_controller');

class FeedbackController extends BaseController {
  /**
   * 提交反馈
   */
  async create() {
    const { ctx } = this;
    const { content, contact } = ctx.request.body;
    
    // 定义校验规则
    const rule = {
      content: {
        type: 'string',
        max: 1000
      },
      contact: {
        type: 'string?',
        max: 100
      }
    };

    try {
      // 使用validate插件进行参数校验
      ctx.validate(rule);
    } catch (error) {
      this.fail(error.message);
      return;
    }
    
    // 获取客户端信息
    const userAgent = ctx.get('user-agent');
    const ip = ctx.ip;
    
    // 调用服务保存反馈
    const result = await ctx.service.feedback.create({
      content,
      contact,
      userAgent,
      ip
    });
    
    if (result) {
      this.success(null, '反馈提交成功');
    } else {
      this.fail('反馈提交失败');
    }
  }
  
  /**
   * TODO：获取反馈列表 (管理员接口)
   */
}

module.exports = FeedbackController;